﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Direct.IDAL;
using Direct.IDAL.User.Jurisdiction;
using DirectSell.Model.User;
using System.Collections;
using DirectSell.Common.InvokeInterface;
using DirectSell.Common.Url;
using DirectSell.Common.Session;

namespace DirectSell.BLL.User.Jurisdiction
{
    /// <summary>
    /// 会员注册服务层
    /// </summary>
    public class RegisterService : UserService
    {
        #region 日志记录--郭文昊--2018年10月7日21:12:21
        /// <summary>
        /// 日志记录文件夹
        /// </summary>
        protected override string LogFile
        {
            get
            {
                return "用户注册";
            }

            set
            {
                base.LogFile = value;
            }
        }
        #endregion

        #region 构造函数--郭文昊--2018年8月19日03:52:12
        /// <summary>
        /// 会员注册数据访问层
        /// </summary>
        protected IRegisterRepository registerRepository { get; set; }
        protected IModifyInfoRespository modifyRepository { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userRepository"></param>
        public RegisterService(IRegisterRepository registerRepository)
        {
            this.registerRepository = registerRepository;
            this.modifyRepository = modifyRepository;
        }
        #endregion

        #region 会员注册操作--郭文昊--2018年8月19日13:06:20
        protected override async Task<UserRespCode> OperationAsync<T>(T param)
        {
            //1.Model验证
            //2.插入注册信息
            //3.提交事务
           

            var model =  CheckModel(param);

            if (model == null)
            {
                return UserRespCode.传入参数不正确;
            }
            //  var result = InvokeInterface.InvokeMethod<UserRespCode, UserInforMationDetails>(model, UrlConfig.DirectSellUrl+"/api/User/Register", "v1.0", "User");
          
            #region  注册信息验证
            if (!registerRepository.ChekConcat(model.RefereeCode))
            {
                return UserRespCode.推荐人不存在或未激活;
            }
            if (!registerRepository.ChekConcat(model.ContactCode))
            {
                return UserRespCode.接点人不存在或未激活;
            }

            if (!registerRepository.CheckServiceCenter(model.ServiceCentreCode))
            {
                return UserRespCode.服务中心不存在;
            }

            if (!registerRepository.CheckUserCode(model.UserCode))
            {
                return UserRespCode.会员编号已存在;
            }

            if (!registerRepository.CheckFirstArea(model.RefereeCode, model.Area))
            {
                return UserRespCode.首次推荐必须在左区;
            }
            if (!registerRepository.CheckPoint(model.ContactCode, model.Area))
            {
                return UserRespCode.该点位上已有会员;
            }
            #endregion

            //记录日志
            await LogAsync(model);
            Hashtable hstable = new Hashtable();
            //提交事务
            InsertMessage(model, ref hstable);
            if (!registerRepository.CommitDapperTranLock(hstable))
            {
                return UserRespCode.失败;
            }
            return UserRespCode.成功;
        }
        #endregion

        #region Model验证--郭文昊--2018年8月19日13:10:35
        /// <summary>
        /// Model验证
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <returns></returns>
        protected UserInforMationDetails CheckModel<T>(T param)
        {
            return param as UserInforMationDetails;
        }
        #endregion

        #region 插入用户注册信息--郭文昊--2018年8月19日13:18:22
        /// <summary>
        /// 插入用户注册信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="hsTable"></param>
        protected void  InsertMessage(UserInforMationDetails model, ref Hashtable hsTable)
        {
            var levelInfo = registerRepository.QueryLevel(model.UserLevel);

            model.LevelName = levelInfo.LevelName;

            model.Investment = levelInfo.Amount;

            model.RefereeId = registerRepository.GetInfo(model.RefereeCode).UserId;

            model.ContactId = registerRepository.GetInfo(model.RefereeCode).UserId;

            registerRepository.InsertRegisterMessage(model,ref hsTable);
        }
        #endregion
    }
}
